<!DOCTYPE html>
<!--[if lt IE 7]>      <html class="no-js lt-ie9 lt-ie8 lt-ie7"> <![endif]-->
<!--[if IE 7]>         <html class="no-js lt-ie9 lt-ie8"> <![endif]-->
<!--[if IE 8]>         <html class="no-js lt-ie9"> <![endif]-->
<!--[if gt IE 8]>      <html class="no-js"> <!--<![endif]-->
<html>
  <head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>quill 编辑器操作</title>
    <meta name="description" content="">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" href="">
    <link href="https://cdn.staticfile.org/quill/1.3.6/quill.snow.css" rel="stylesheet">
    <!-- 引入Quill -->
   <script src="https://cdn.staticfile.org/quill/1.3.6/quill.js"></script>
   <style>
     #editor .AtEmbed{
       color: #0000ffe2;
     }
   </style>
  </head>
  <body>
    <!--[if lt IE 7]>
      <p class="browsehappy">You are using an <strong>outdated</strong> browser. Please <a href="#">upgrade your browser</a> to improve your experience.</p>
    <![endif]-->
    
   <!-- 创建容器 -->
    <button id="setBtn"> 插入按钮</button>
    <button id="getBtn"> 获取当前内容按钮</button>
    <button id="updateBtn"> 修改编辑器的内容</button>

    <button id="insertBtn"> 插入自定义的内容</button>

    <div id="editor">
  
    </div>

    <script>
     // 定义化@  插入
     const Embed = Quill.import("blots/embed");
     class AtEmbed extends Embed {
        static create (userInfo) {
          let node = super.create(userInfo)
          node.setAttribute('data-key', userInfo.uid)
          node.setAttribute("contenteditable", "false");
          node.innerHTML = `@${userInfo.name}`
          return node
        }

        static value (node) {
          return node.dataset.key
        }

        static blotName = 'AtEmbed'
        static className = 'AtEmbed'
        static tagName = 'span'
      }
      Quill.register(AtEmbed, true);
    </script>
    <script>
  
    var quill = new Quill('#editor', {
      theme: 'snow'
    });

    setBtn.onclick = function(){
      // 设置内容，带有一种清空的以为，重新覆盖，从头赋值
      quill.setContents([
        { insert: 'Hello ', attributes: { color: 'red' } },
        { insert: 'World!', attributes: { bold: true } },
        { insert: '\n' }
      ]);
    }

    getBtn.onclick = function(){
      console.log(quill.getContents());
    }
    updateBtn.onclick = function(){
       // quill 中已经存在了节点，进行更新修改  retain 保留  delete  删除
       quill.updateContents([
          { "retain": 6 },
          { "retain": 5, "attributes": { "color": "#ff0000" } }
        ])
    }
    insertBtn.onclick = function(){
      quill.insertEmbed( 0, "AtEmbed", {uid: 111, name:'张三'});
    }
    
    </script>

  </body>
</html>